<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="入职日期" prop="joinTime">
        <el-date-picker
          clearable
          v-model="queryParams.joinTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择入职日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="部门" prop="depart">
        <el-input
          v-model="queryParams.depart"
          placeholder="请输入部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="外包单位" prop="depart">
        <el-select v-model="queryParams.companyId" placeholder="请选择外包单位">
            <el-option v-for="item in unitList" :key="item.companyId" :label="item.companyName" :value="item.companyId"></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="现任岗位" prop="currentPosition">
        <el-input
          v-model="queryParams.currentPosition"
          placeholder="请输入现任岗位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学历" prop="degree">
        <el-input
          v-model="queryParams.degree"
          placeholder="请输入学历"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="人员姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入人员姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="家庭住址" prop="homeAddress">
        <el-input
          v-model="queryParams.homeAddress"
          placeholder="请输入家庭住址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['app:outemployee:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['app:outemployee:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['app:outemployee:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['app:outemployee:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="outemployeeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="${comment}" align="center" prop="userId" /> -->
      <el-table-column label="人员姓名" align="center" prop="userName" />

      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          {{ scope.row.sex == 1 ? "男" : "女" }}
        </template>
      </el-table-column>
      <el-table-column
        label="入职日期"
        align="center"
        prop="joinTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.joinTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="部门" align="center" prop="depart" />
      <el-table-column label="外包单位" align="center" prop="companyName" />
      <el-table-column label="现任岗位" align="center" prop="currentPosition" />
      <el-table-column label="身份证号" align="center" prop="idCard" />
      <el-table-column label="电话号码" align="center" prop="phone" />
      <el-table-column label="人员照片" align="center" prop="photo">
        <template slot-scope="scope">
          <image-preview :src="scope.row.photo" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="学历" align="center" prop="degree" />
      <el-table-column label="工种类型" align="center" prop="workType" />
      <el-table-column label="家庭住址" align="center" prop="homeAddress" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['app:outemployee:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['app:outemployee:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改外包人员信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="人员姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入人员姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option label="男" :value="1"> </el-option>
            <el-option label="女" :value="0"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="入职日期" prop="joinTime">
          <el-date-picker
            clearable
            v-model="form.joinTime"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择入职日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="部门" prop="depart">
          <!-- <el-input v-model="form.depart" placeholder="请输入部门" /> -->
          <el-cascader
            placeholder="部门"
            v-model="form.depart"
            :options="deptOptions"
            :props="{
              value: 'label',
              multiple: false,
              emitPath: false,
              expandTrigger: 'hover',
            }"
            :show-all-levels="false"
            filterable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="外包单位" prop="companyName">
          <el-select v-model="form.companyId" placeholder="请选择外包单位">
            <el-option v-for="item in unitList" :key="item.companyId" :label="item.companyName" :value="item.companyId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="现任岗位" prop="currentPosition">
          <el-input
            v-model="form.currentPosition"
            placeholder="请输入现任岗位"
          />
        </el-form-item>
        <el-form-item label="工种类型" prop="workType">
          <el-input v-model="form.workType" placeholder="请输入工种类型" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="电话号码" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话号码" />
        </el-form-item>
        <el-form-item label="人员照片" prop="photo">
          <!-- <el-input v-model="form.photo" placeholder="请输入人员照片" /> -->
          <ImageUpload
            v-model="form.photo"
            @input="upImage"
            :limit="1"
          ></ImageUpload>
        </el-form-item>
        <el-form-item label="学历" prop="degree">
          <!-- <el-input v-model="form.degree" placeholder="请输入学历" /> -->
          <el-select v-model="form.degree" placeholder="请选择学历">
            <el-option label="小学" value="小学"> </el-option>
            <el-option label="初中" value="初中"> </el-option>
            <el-option label="中专" value="中专"> </el-option>
            <el-option label="高中" value="高中"> </el-option>
            <el-option label="大专" value="大专"> </el-option>
            <el-option label="本科" value="本科"> </el-option>
            <el-option label="研究生" value="研究生"> </el-option>
            <el-option label="博士" value="博士"> </el-option>
            <el-option label="硕士" value="硕士"> </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="家庭住址" prop="homeAddress">
          <el-input v-model="form.homeAddress" placeholder="请输入家庭住址" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import {
  listOutemployee,
  getOutemployee,
  delOutemployee,
  addOutemployee,
  updateOutemployee,
} from "@/api/app/outemployee";
import { deptTreeSelect, listUser } from "@/api/system/user";
import { listUnit} from "@/api/system/outunit";
export default {
  name: "Outemployee",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 外包人员信息表格数据
      outemployeeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sex: null,
        joinTime: null,
        depart: null,
        currentPosition: null,
        idCard: null,
        phone: null,
        photo: null,
        degree: null,
        userName: null,
        workType: null,
        homeAddress: null,
        companyName:null,
        companyId:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      deptOptions: [],
      unitList:[]
    };
  },
  created() {
    this.getList();
    this.getUnitList()
  },
  methods: {
    /** 查询外包人员信息列表 */
    getList() {
      this.loading = true;
      listOutemployee(this.queryParams).then((response) => {
        this.outemployeeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sex: null,
        joinTime: null,
        depart: null,
        currentPosition: null,
        idCard: null,
        phone: null,
        photo: null,
        degree: null,
        userName: null,
        workType: null,
        homeAddress: null,
        remark: null,
        companyName:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.userId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.getDeptTree();
      this.reset();
      this.open = true;
      this.title = "添加外包人员信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.getDeptTree();
      this.reset();
      const userId = row.userId || this.ids;
      getOutemployee(userId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改外包人员信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.userId != null) {
            updateOutemployee(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOutemployee(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$modal
        .confirm('是否确认删除外包人员信息编号为"' + userIds + '"的数据项？')
        .then(function () {
          return delOutemployee(userIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "app/outemployee/export",
        {
          ...this.queryParams,
        },
        `outemployee_${new Date().getTime()}.xlsx`
      );
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    getUnitList(){
        listUnit({pageNum: 1,pageSize: 100}).then(res => {
            // console.log(res);
            this.unitList = res.rows;
        });
    },
    upImage(e) {
      this.form.photo = e;
    },
  },
};
</script>
<style lang="scss" scoped>
.el-dialog__wrapper {
  .el-dialog {
    .el-dialog__body {
      height: 30% !important;
    }
  }
}
</style>